package com.cyj.dream.admin.controller.admin.system;

import com.cyj.dream.admin.service.system.SysRolePermissionService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 角色权限关联关系控制器
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 11:56
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "rolePermission", tags = "角色权限关联关系模块")
@RequestMapping(value = "/pc/rolePermission", name = "角色权限关联关系模块")
public class SysRolePermissionController {

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 为角色设置权限
     *
     * @param sysRoleId       角色id
     * @param sysPermissionId 权限id集合
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-5-18 08:55:34
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为角色设置权限", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRoleId", value = "角色id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "sysPermissionId", value = "权限id集合", dataType = "List<Long>", required = true)
    })
    @LogRecord(title = "为角色设置权限", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/settingSysRolePermission", name = "为角色设置权限", method = RequestMethod.POST)
    public Map<String, Object> settingSysRolePermission(Long sysRoleId, List<Long> sysPermissionId) {
        log.info("进入为角色设置权限方法控制器，角色id：{}，权限id集合:{}", sysRoleId, sysPermissionId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRoleId) || IsEmptyUtils.isEmpty(sysPermissionId)) {
            log.error("出现错误：参数缺失为角色设置权限失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysRolePermissionService.settingSysRolePermission(sysRoleId, sysPermissionId));
        return map;
    }

    /**
     * 为角色移除权限
     *
     * @param sysRoleId       角色id
     * @param sysPermissionId 权限id集合
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-5-18 08:55:34
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为角色移除权限", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRoleId", value = "角色id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "sysPermissionId", value = "权限id集合", dataType = "List<Long>", required = true)
    })
    @LogRecord(title = "为角色移除权限", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/deleteSysRolePermission", name = "为角色移除权限", method = RequestMethod.DELETE)
    public Map<String, Object> deleteSysRolePermission(Long sysRoleId, List<Long> sysPermissionId) {
        log.info("进入为角色移除权限方法控制器，角色id：{}，权限id集合:{}", sysRoleId, sysPermissionId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRoleId) || IsEmptyUtils.isEmpty(sysPermissionId)) {
            log.error("出现错误：参数缺失为用户移除角色失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysRolePermissionService.deleteSysRolePermission(sysRoleId, sysPermissionId));
        return map;
    }

}
